package com.syn.michat.im.service.impl;

import com.alibaba.fastjson.JSON;
import com.syn.michat.constant.ErrorContants;
import com.syn.michat.constant.MessageContants;
import com.syn.michat.im.WebSocketSessionManager;
import com.syn.michat.im.model.MessageEntity;
import com.syn.michat.im.model.Package;
import com.syn.michat.im.service.MessageService;
import com.syn.michat.im.service.PackageService;
import com.syn.michat.mapper.MessageMapper;
import com.syn.michat.pojo.OfflineMsg;
import com.syn.michat.pojo.User;
import com.syn.michat.service.UserService;
import com.syn.michat.util.BlowfishUtil;
import com.syn.michat.util.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private UserService userService;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private PackageService packageService;

    @Override
    public void sendMsg(String senderId, String targetId, String msgType) throws Exception {
        User sender = userService.findUser(senderId);
        User receiver = userService.findUser(targetId);
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        MessageEntity msg = new MessageEntity();
        msg.setUuid(uuid);
        msg.setMsgType(msgType);
        msg.setSender(sender);
        msg.setReceiver(receiver);
        msg.setTargetId(senderId);
        msg.setTime(timestamp.getTime());

        Package aPackage = new Package();
        aPackage.setType(Package.PACKAGE_TYPE_MESSAGE);
        aPackage.setData(BlowfishUtil.encrypt(JSON.toJSONString(msg)));

        Session session = WebSocketSessionManager.sessionMap.get(targetId);
        packageService.sendPackage(aPackage, session);
    }

    @Override
    public void saveOfflineMsg(String packageStr, MessageEntity msg)  throws ClientException {
        OfflineMsg oMsg = new OfflineMsg();
        oMsg.setReceiverId(msg.getReceiver().getUserId());
        oMsg.setPackageStr(packageStr);
        oMsg.setSendTime(msg.getTime());
        oMsg.setStatus(MessageContants.OFFLINE_MESSAGE_NOT_SENT);

        try {
            messageMapper.add(oMsg);
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public List<OfflineMsg> findByReceiverId(String receiverId, int status) throws ClientException {
        try {
            return messageMapper.findByReceiverId(receiverId, status);
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public void updateStatus(int id, int status) throws ClientException {
        try {
            messageMapper.updateStatus(id, status);
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }
}
